//
//  MSPswChangeViewController.m
//  MinShengBank
//
//  Created by chen wei on 13-10-11.
//  Copyright (c) 2013年 Hq. All rights reserved.
//

#import "MSPswChangeViewController.h"
#import "RequestServer.h"
#import "RequestServerDelegate.h"
#import "BSAppDelegate.h"
#import "HILoginViewController.h"
#import <CommonCrypto/CommonDigest.h>

@interface MSPswChangeViewController ()<RequestServerDelegate,UITextFieldDelegate>

@end

@implementation MSPswChangeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    [super viewWillAppear:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"f0f0f0"];
	UIView *loginView;
    if (isIOS7) {
        loginView = [[UIView alloc] initWithFrame:CGRectMake(0, 68, 320, 200)];
    } else loginView = [[UIView alloc] initWithFrame:CGRectMake(0, 28, 320, 200)];
    loginView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:loginView];
    
    UILabel *accountLabel;
    accountLabel=[[UILabel alloc]initWithFrame:CGRectMake(25, 0, 100, 32)];
    accountLabel.textAlignment = NSTextAlignmentLeft;
    accountLabel.font=[UIFont systemFontOfSize:18.0];
    accountLabel.backgroundColor = [UIColor whiteColor];
    accountLabel.alpha = 0.8;
    accountLabel.text=@"原密码:";
    [loginView addSubview:accountLabel];
    
    UILabel *passLabel;
    passLabel=[[UILabel alloc]initWithFrame:CGRectMake(25, 52, 100, 32)];
    passLabel.textAlignment = NSTextAlignmentLeft;
    passLabel.font=[UIFont systemFontOfSize:18.0];
    passLabel.backgroundColor = [UIColor whiteColor];
    passLabel.alpha = 0.8;
    passLabel.text =@"新密码:";
    [loginView addSubview:passLabel];
    
    UILabel *newpassLabel;
    newpassLabel=[[UILabel alloc]initWithFrame:CGRectMake(25, 104, 100, 32)];
    newpassLabel.textAlignment = NSTextAlignmentLeft;
    newpassLabel.font=[UIFont systemFontOfSize:18.0];
    newpassLabel.backgroundColor = [UIColor whiteColor];
    newpassLabel.alpha = 0.8;
    newpassLabel.text =@"确认密码:";
    [loginView addSubview:newpassLabel];
    
    self.accountField = [[UITextField alloc] initWithFrame:CGRectMake(125, 0, 165, 32)];
    self.accountField.backgroundColor = [UIColor whiteColor];
    self.accountField.returnKeyType = UIReturnKeyDone;
    self.accountField.delegate = self;
    self.accountField.placeholder = @"请输入原密码";
    self.accountField.alpha = 0.8;
    self.accountField.secureTextEntry = YES;
    self.accountField.textColor = [UIColor colorWithHexString:@"6d6d6d"];
    self.accountField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.accountField.font = [UIFont systemFontOfSize:18.0];
    [loginView addSubview:self.accountField];
    
    UIImageView *xian = [[UIImageView alloc]initWithFrame:CGRectMake(0, 40, 320, 1)];
    xian.image = [UIImage imageNamed:@"lanxian.png"];
    [loginView addSubview:xian];

    
    self.passField = [[UITextField alloc] initWithFrame:CGRectMake(125, 52, 165, 32)];
    self.passField.backgroundColor = [UIColor whiteColor];
    self.passField.returnKeyType = UIReturnKeyDone;
    self.passField.delegate = self;
    self.passField.alpha = 0.8;
    self.passField.secureTextEntry = YES;
    self.passField.placeholder = @"请输入新密码";
    self.passField.textColor = [UIColor colorWithHexString:@"6d6d6d"];
    self.passField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.passField.font = [UIFont systemFontOfSize:18.0];
    [loginView addSubview:self.passField];
    
    UIImageView *xian1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 90, 320, 1)];
    xian1.image = [UIImage imageNamed:@"lanxian.png"];
    [loginView addSubview:xian1];

    
    self.newpassField = [[UITextField alloc] initWithFrame:CGRectMake(125, 104, 165, 32)];
    
    self.newpassField.backgroundColor = [UIColor whiteColor];
    self.newpassField.returnKeyType = UIReturnKeyDone;
    self.newpassField.delegate = self;
    self.newpassField.alpha = 0.8;
    self.newpassField.secureTextEntry = YES;
    self.newpassField.placeholder = @"请再输一次";
    self.newpassField.textColor = [UIColor colorWithHexString:@"6d6d6d"];
    self.newpassField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.newpassField.font = [UIFont systemFontOfSize:18.0];
    [loginView addSubview:self.newpassField];
    
    
    UIImageView *xian2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 146, 320, 1)];
    xian2.image = [UIImage imageNamed:@"lanxian.png"];
    [loginView addSubview:xian2];

   
    
    UIButton *logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [logoutButton setFrame:CGRectMake(10, 167, 300, 39)];
    [logoutButton setTitle:@"确 定" forState:UIControlStateNormal];
    logoutButton.backgroundColor = [UIColor colorWithHexString:@"00b5ed"];
    [logoutButton addTarget:self action:@selector(changePass:) forControlEvents:UIControlEventTouchUpInside];
    [loginView addSubview:logoutButton];
    
    self.title = @"修改密码";
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 0, 30, 30);
    [button setImage:[UIImage imageNamed:@"返回.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

#pragma mark 返回
-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)changePass:(id)sender
{
    
    [self.accountField resignFirstResponder];
    [self.passField resignFirstResponder];
    [self.newpassField resignFirstResponder];
    if ([self.accountField.text isEqualToString:@""]||[self.passField.text isEqualToString:@""]||[self.newpassField.text isEqualToString:@""]) {
        ShowMessage(@"输入信息不完整，请重新输入");
        return;
    }
    if (![self.passField.text isEqualToString:self.newpassField.text]) {
        ShowMessage(@"新密码两次输入不一致，请重新输入");
        return;
    }
    NSDictionary *para = [NSDictionary dictionaryWithObjectsAndKeys:[self md5:self.accountField.text],@"oldpwd",[self md5:self.passField.text], @"newpwd",[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"],@"userid",nil];
    [[RequestServer instance] doActionAsync:0 withAction:@"LoginAction$changePwd" withParameters:para withDelegate:self];
}

-(NSString *)md5:(NSString *)str {
    const char *cStr = [str UTF8String];
    unsigned char result[32];
    CC_MD5( cStr, strlen(cStr), result );
    return [NSString stringWithFormat:
            @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}


- (void)requestFinished:(NSNumber *)code withMessage:(NSString *)message withData:(id)data
{
    if (code==0) {
        ShowMessage(@"修改密码成功！");
        
        BSAppDelegate *app =  (BSAppDelegate*)[[UIApplication sharedApplication] delegate];
        [app.navigationController popToRootViewControllerAnimated:YES];

        
    } else if ([code intValue]==1){
        ShowMessage(message);
    }else{
        ShowMessage(@"修改密码失败，请重新修改");
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
